/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_graphs.h"
#include "rep_logs_pages.h"
#include "rep_general.h"
#include "GR_write_html.h"

void
write_body_graphvis_html_file(GraphFrame *gf,FILE *fp,int ngraphs,
			      int nledges,WebStatPtr msn);

void
write_body_uservis_html_file(GraphFrame *gf,FILE *fp,int nusers, int nledges,
			     WebStatPtr msn);

int 
report_graphs_most_visited(GraphFrame *gf, char *name_file,int ngraphs, 
			   int nledges, WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Most Visited Graphs");
  write_start_body_html_file(fp,"#ffffff");
  write_style_html_file(fp);
  write_body_graphvis_html_file(gf,fp,ngraphs,nledges,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_graphvis_html_file(GraphFrame *gf,FILE *fp,int ngraphs,
			      int nledges,WebStatPtr msn)
{
  LNode_type *lgraphs = LOGR_get_list_graphs(msn);
  LNode_type *lusers = LOGR_get_list_users(msn);
  LNode_type *ptr = NULL;
  time_t now;
  WebUgraphPtr *varray = NULL;
  int tgraphs = lgraphs ? count_llist(lgraphs) : 0;
  int tusers = lusers ? count_llist(lusers) : 0;
  int tnedges = 0;
  int tn = 0;
  int i = 0,j=0;
  char ntitle[100];
  long thits = LOGR_get_total_site_naccesses(msn);
  char *scolor = "beige";

  for(ptr = lgraphs->back; ptr != lgraphs; ptr=ptr->back)
  {
    WebUgraphPtr gn = (WebUgraphPtr)ptr->info;

    int nedges = LOGR_get_ugraph_number_edges(gn);

    if(nedges >=nledges)
	tnedges++;
  } 

  varray = tnedges ? (WebUgraphPtr*)calloc(tnedges,
					   sizeof(WebUgraphPtr)) : NULL;

  if(!varray && tnedges > 0) {
      fprintf(stderr,"Out of Memory write_body_graphvis_html_file\n");
      abort();
  }

  for(ptr = lgraphs->back; ptr != lgraphs; ptr=ptr->back)
  {
    WebUgraphPtr gn = (WebUgraphPtr)ptr->info;
    int nedges = LOGR_get_ugraph_number_edges(gn);

    if(nedges >=nledges)
	varray[i++]=gn;
  } 

  now = time(NULL);
  write_header_html_file(fp,1,"List of Graphs with most User Sessions");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Graphs : ");
  fprintf(fp, " %d \n",tgraphs);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Users : ");
  fprintf(fp, " %d \n",tusers);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Minimum Number of Edges : ");
  fprintf(fp, " %d \n",nledges);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RMVGString);
  write_horizontal_rule_html_file(fp);

  if(tnedges <= 0) 
      return;

  i = 0;

  if(ngraphs > tnedges)
      tn = tnedges;
  else
      tn = ngraphs;

  sprintf(ntitle,"%d Graphs with most User Sessions", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string2(fp,"Num","Graph","Users");

  for(i = 1; i<=tn ; i++)
  {
    char *pcolor = i%2 ? scolor : NULL;
    WebUgraphPtr gn = (WebUgraphPtr)randomized_select((GPTR *)varray,
				       i-1,tnedges-1,1,
				       LOGR_is_ugraph_access_lower,
				       LOGR_is_ugraph_access_bigger);

    if(gn)
    {
	int nedges = LOGR_get_ugraph_number_edges(gn);
	WebGenLinkPtr gln = 0;
	long nacc = LOGR_get_ugraph_naccesses(gn);
	char snum[20],scount[20];

	sprintf(snum,"%d", i);
	sprintf(scount,"%ld",nacc);
	    
	for(j=0;j<nedges; j++)
	{
	    gln = (WebGenLinkPtr)LOGR_get_ugraph_gen_edge(gn,j);
	    if(gln) 
	    {
		const char *utit = LOGR_get_gen_title_source(gln);
		const char *vtit = LOGR_get_gen_title_target(gln);
		char *ula = (char *)LOGR_get_gen_url_source(gln);
		char *vla = (char *)LOGR_get_gen_url_target(gln);
		if(j == 0)
		    write_row_span_num_hyperlink_string_color(fp,snum, ula,
				    utit,vla,vtit,NULL,scount,nedges,pcolor);
		else
		    write_row_span_num_hyperlink_string_color(fp,NULL, ula,
				     utit,vla,vtit,NULL,NULL,nedges,pcolor);
	    }
	}

    }
  }
  write_end_table_html_file(fp); 

  free(varray); varray = NULL; 
}

int 
report_users_most_requests(GraphFrame *gf, char *name_file,int nusers,
			   int nledges,WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Users with Longest Session");
  write_start_body_html_file(fp,"#ffffff");
  write_style_html_file(fp);
  write_body_uservis_html_file(gf,fp,nusers,nledges,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_uservis_html_file(GraphFrame *gf,FILE *fp,int nusers, int nledges,
			     WebStatPtr msn)
{
  LNode_type *lgraphs = LOGR_get_list_graphs(msn);
  LNode_type *lusers = LOGR_get_list_users(msn);
  LNode_type *ptr = NULL;
  time_t now;
  WebUgraphPtr *varray = NULL;
  int tgraphs = lgraphs ? count_llist(lgraphs) : 0;
  int tusers = lusers ? count_llist(lusers) : 0;
  int tnedges = 0;
  int tn = 0;
  int i = 0,j = 0;
  char ntitle[100];
  long thits = LOGR_get_total_site_naccesses(msn);
  char *scolor = "beige";

  for(ptr = lgraphs->back; ptr != lgraphs; ptr=ptr->back)
  {
    WebUgraphPtr gn = (WebUgraphPtr)ptr->info;

    int nedges = LOGR_get_ugraph_number_edges(gn);

    if(nedges <= nledges)
	tnedges++;
  } 

  varray = tnedges ? (WebUgraphPtr*)calloc(tnedges,
					   sizeof(WebUgraphPtr)) : NULL;

  if(!varray && tnedges > 0) {
      fprintf(stderr,"Out of Memory write_body_uservis_html_file\n");
      abort();
  }

  for(ptr = lgraphs->back; ptr != lgraphs; ptr=ptr->back)
  {
    WebUgraphPtr gn = (WebUgraphPtr)ptr->info;
    int nedges = LOGR_get_ugraph_number_edges(gn);

    if(nedges <= nledges)
	varray[i++]=gn;
  } 

  now = time(NULL);
  write_header_html_file(fp,1,"List of Users with Longest Session");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Graphs : ");
  fprintf(fp, " %d \n",tgraphs);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Users : ");
  fprintf(fp, " %d \n",tusers);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Maximum Number of Edges : ");
  fprintf(fp, " %d \n",nledges);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RMVUString);
  write_horizontal_rule_html_file(fp);

  if(tnedges <= 0) 
      return;

  i = 0;

  if(nusers > tnedges) 
      tn = tnedges;
  else
      tn = nusers;

  sprintf(ntitle,"%d Users with Longest Session", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string5(fp,"Num","User","First Referer","Entry Page",
			    "Graph","Requests");

  for(i = 1; i<=tn ; i++)
  {
    char *pcolor = i%2 ? scolor : NULL;
    WebUgraphPtr gn = (WebUgraphPtr)randomized_select((GPTR *)varray,
				       i-1,tnedges-1,1,
				       LOGR_is_ugraph_number_edges_lower,
				       LOGR_is_ugraph_number_edges_bigger);

    if(gn)
    {
	int nedges = LOGR_get_ugraph_number_edges(gn);
	WebGenLinkPtr gln = 0;
	WebUserPtr usn = 0;
	LNode_type *uptr = NULL;
	LNode_type *lgusers = LOGR_get_ugraph_lusers(gn);
	/*long nacc = LOGR_get_ugraph_naccesses(gn);*/
	int cnu = LOGR_get_ugraph_num_users(gn);
	char *rla = NULL, *ela = NULL, *etit = NULL;
	char *suser = NULL;
	char snum[20], scount[20];
	int nspan = (cnu > nedges) ? cnu : nedges;
	int mspan = (cnu < nedges) ? cnu : nedges;
	int dspan = nspan - mspan;

	sprintf(snum,"%d", i);
	sprintf(scount,"%d",nedges);

	if(!lgusers)
	    continue;

	uptr = lgusers->back;
	    
	for(j=0;j<nspan; j++)
	{
	  char *utit = NULL, *vtit = NULL;
	  char *ula = NULL, *vla = NULL;
	  
	  if(uptr != lgusers)
	  {
	      usn = (WebUserPtr)uptr->info;
	      uptr = uptr->back;
	  }

	  if(usn)
	  {
	      rla = LOGR_get_user_referer(usn);
	      ela = LOGR_get_user_entry_page(usn);
	      etit = (char *)LOGR_get_user_entry_page_title(usn,gf);
	      suser = LOGR_get_user_name(usn);
	  }

	  gln = (j < nedges) ? 
	      (WebGenLinkPtr)LOGR_get_ugraph_gen_edge(gn,j) : NULL;
	  
	  if(gln) 
	  {
	    utit = (char *)LOGR_get_gen_title_source(gln);
	    vtit = (char *)LOGR_get_gen_title_target(gln);
	    ula = (char *)LOGR_get_gen_url_source(gln);
	    vla = (char *)LOGR_get_gen_url_target(gln);
	  }

	  if(j == 0)
	    write_row_user_graph_table(fp,snum,suser,rla,ela,etit,ula,utit,
				       vla,vtit,NULL,scount,nspan,pcolor);
	  else if(j<mspan)
	    write_row_user_graph_table(fp,NULL,suser,rla,ela,etit,ula,utit,
				       vla,vtit,NULL,NULL,nspan,pcolor);
	  else if(j==mspan && j==cnu)
	    write_row_user_graph_table(fp,NULL,"","","","",ula,utit,
				       vla,vtit,NULL,NULL,dspan,pcolor);
	  else if(j==mspan && j==nedges)
	    write_row_user_graph_table(fp,NULL,suser,rla,ela,etit,"","",
				       "","",NULL,NULL,dspan,pcolor);
	  else if (j < nedges )
	    write_row_user_graph_table(fp,NULL,NULL,NULL,NULL,NULL,ula,utit,
				       vla,vtit,NULL,NULL,nspan,pcolor);
	  else if (j < cnu )
	    write_row_user_graph_table(fp,NULL,suser,rla,ela,etit,NULL,NULL,
				       NULL,NULL,NULL,NULL,nspan,pcolor);
	}
    }
  }
  write_end_table_html_file(fp); 

  free(varray); varray = NULL; 
}




